package com.hedl.practice.diyJava.network_programming.http_programming;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;

public class Http_Study {
    static HttpClient httpClient = HttpClient.newBuilder().build();

    private static void httpGet(String url)throws Exception{
        HttpRequest request = HttpRequest.newBuilder(new URI(url))
                //设置Header
                .header("User-Agent","Java HttpClient")
                .header("Accept","*/*")
                //设置超时
                .timeout(Duration.ofSeconds(5))
                //设置版本
                .version(HttpClient.Version.HTTP_2)
                .build();
        HttpResponse<String> response = httpClient.send(request,HttpResponse.BodyHandlers.ofString());
        //System.out.println(response.body().substring(0,2048)+"...");      //读取固定字节
        //System.out.println(response.body()+"...");        //读取全部，然后输出到控制台

        //写入到指定文件夹一份
        try(FileWriter fw = new FileWriter("/Users/a101/idea/新浪网页备份.html")){
            //fw.write(response.body().substring(0,1024));
            fw.write(response.body());
        }
    }

    private static void httpGetImage(String url)throws Exception{
        HttpRequest request = HttpRequest.newBuilder(new URI(url))
                //设置Header
                .header("User-Agent","Java HttpClient")
                .header("Accept","*/*")
                //设置超时
                .timeout(Duration.ofSeconds(5))
                //设置版本
                .version(HttpClient.Version.HTTP_2)
                .build();
        HttpResponse<InputStream> response = httpClient.send(request,HttpResponse.BodyHandlers.ofInputStream());
        //显示Http返回的图片
        BufferedImage img = ImageIO.read(response.body());
        ImageIcon icon = new ImageIcon(img);
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());
        frame.setSize(200,100);
        JLabel lbl = new JLabel();
        lbl.setIcon(icon);
        frame.add(lbl);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private static void httpPost(String url,String body)throws Exception{
        HttpRequest request = HttpRequest.newBuilder(new URI(url))
                //设置Header
                .header("User-Agent","Mozilla/5.0 (compatible, MSIE 11, Windows NT 6.3; Trident/7.0) like Gecko")
                .header("Accept","*/*")
                .header("Content-Type", "application/x-www-form-urlencoded")
                //设置超时
                .timeout(Duration.ofSeconds(5))
                //设置版本
                .version(HttpClient.Version.HTTP_2)
                //使用POST并设置Body
                .POST(HttpRequest.BodyPublishers.ofString(body, StandardCharsets.UTF_8))
                .build();
        HttpResponse<String> response = httpClient.send(request,HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }

    public static void main(String[] args) throws Exception{
        httpGet("https://www.sina.com.cn");
        System.out.println("-----------------------");
        httpPost("https://accounts.douban.com/j/mobile/login/basic","name=bob%40example.com&password=12345678&remember=false&ck=&ticket=");
        System.out.println("-----------------------");
        httpGetImage("https://img.t.sinajs.cn/t6/style/images/global_nav/WB_logo.png");

        try(FileReader fr = new FileReader("/Users/a101/idea/新浪网页备份.html");BufferedReader br = new BufferedReader(fr)){
            String line;
            System.out.println("从备份的指定文件夹下读取文件：\n");
            while ((line = br.readLine()) != null){
                System.out.println(line);
            }
        }
    }
}
